package Reflect.demo01;


import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/*
    通过字节码对象操作构造函数
 */
public class ReflectDemo04 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Class<?> aClass=Class.forName("Reflect.Book");

        //通过字节码对象得到构造函数的对象
        Constructor<?> constructor1=aClass.getConstructor();//无参
        Constructor<?> constructor2=aClass.getConstructor(int.class,String.class);//有参

        //通过构造函数对象创建该类的实例
        Book book1=(Book)constructor1.newInstance();
        Book book2=(Book)constructor2.newInstance(1,"cf");

        System.out.println(book1);
        System.out.println(book2);
    }
}
